﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace FlickrWP7.Core
{
    public class Blogs
    {
        private HelperDelegate helperDelegateInstance;

        public string LastBlogResult { get; set; }

        public void GetServiceList(string apiKey, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.blogs.getServices&api_key={0}&format=json",
                    apiKey);

            client.DownloadStringAsync(new Uri(URL));
        }

        public void GetBlogList(string apiKey, string authToken, string signature, HelperDelegate helperDelegate,string service="")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.blogs.getList&api_key={0}&api_sig={1}&auth_token={2}&format=json",
                    apiKey,signature,authToken);

            if (service != "")
                URL += "&service=" + service;

            client.DownloadStringAsync(new Uri(URL));
        }

        public void PostPhoto(string apiKey, string authToken, string signature, string photoID,
            string title, string description, string blogID, HelperDelegate helperDelegate, 
            string blogPassword="",string blogService ="")
        {
            helperDelegateInstance = helperDelegate;
            string URL = string.Format(
                "http://www.flickr.com/services/rest/?method=flickr.blogs.postPhoto&api_key={0}&api_sig={1}&auth_token={2}&photo_id={3}&title={4}&description={5}&format=json",
                apiKey, signature, authToken, photoID, title, description);

            if (blogPassword != "")
                URL += "&blog_password=" + blogPassword;
            if (blogService != "")
                URL += "&blog_service=" + blogService;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";

            request.BeginGetResponse(asyncCallback =>
            {
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncCallback);
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    LastBlogResult = reader.ReadToEnd();
                }
                helperDelegate();
            },null);
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            LastBlogResult = e.Result;
            helperDelegateInstance();
        }
    }
}
